<?php
namespace LPCISII\PHP2012Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use LPCISII\PHP2012Bundle\Entity\Articles;
use LPCISII\PHP2012Bundle\Entity\Commandes;
use Symfony\Component\Security\Core\SecurityContext;



class CommandeController extends Controller {
    
  function purchaseAction($validation = 0){
    $connected = is_object($this->get('security.context')->getToken()->getUser());
    if($connected){
      
    
      $em = $this->getDoctrine()->getEntityManager();
      $ar = $em->getRepository('LPCISIIPHP2012Bundle:Articles');
      $panier = array();
      $recapitulatif = array();
      $total = 0;
      $livraison = 0;
      $prix = 0;
      $statut = 0;
      $errors = array();
      $lastCommande = null;
      if($this->getRequest()->cookies->has("panier")){
	$seriapanier = $this->getRequest()->cookies->get("panier");
	$panier = unserialize($seriapanier);
      	
	  if(!empty($panier)){
	 
	  $poids = 0;
	  foreach ($panier as $key => $art){
	    $article = $ar->findByReference($art['id']);
	    if (!empty($article)){
	      $recapitulatif [] = array('id'=>$article[0]->getReference(),'nom'=>$article[0]->getNom(),'prix'=>$article[0]->getPrix(), 'qte'=>$art['qte']);
		$prix += $art['qte'] * $article[0]->getPrix();
		$poids += $article[0]->getPoids();	  
	    } else {
	      $recapitulatif [] = array('id'=>'Article supprimé','nom'=>'/','prix'=>'/','qte'=>'/');
	    }
	    
	   switch($poids){
	    case($poids <= 5):
	       $livraison = 2.5;
	       break;
	   case($poids > 5 && $poids <= 10):
	      $livraison = 5;
	      break;
	   case($poids > 10):
	     $livraison = 10;
	     break;
	    }
	   $total = $prix + $livraison;
	  }
	  //chercher dernière commande
	  $client = $this->get('security.context')->getToken()->getUser();
	  $idClient = $client->getIdU();
	  $lastCommande = $em->getRepository('LPCISIIPHP2012Bundle:Commandes')
	    ->getLastCommande($idClient);
	  
          $parameters = array();
          
	  if(!empty($lastCommande[0])){
            $parameters['lastCommande'] = $lastCommande[0];
	    $statut = $lastCommande[0]->getEtat();
	    }
	  
	    if($validation == 1){
	      
	      $v = $this->container->get('doctrine')->getEntityManager();
	      $c = new Commandes();
	      
	      $c->setUtilisateur($client);
	      $c->setPrix($total);
	      $c->setEtat(0);
	      $c->setDate(new \DateTime("now"));
	      $c->addUtilisateur($client);
	      $v->persist($c);
	      $v->flush();
	      $parameters['lastCommande'] = $c;
	    }
	  
	}
      }
      $parameters['recapitulatif'] = $recapitulatif;
      $parameters['prix'] = $prix;
      $parameters['livraison'] = $livraison;
      $parameters['total'] = $total;
      $parameters['errors'] = $errors;
      $parameters['statut'] = $statut;
      
      // si on vient d'annuler la commande
      if($validation == 2) {
          $parameters['panier'] = $panier;
          return $this->render('LPCISIIPHP2012Bundle:Page:panier.html.twig', $parameters);
      } else {
          return $this->render('LPCISIIPHP2012Bundle:Page:purchase.html.twig', $parameters);
      }
    } else { return ($this->render('LPCISIIPHP2012Bundle:Security:login.html.twig', array(
        'redirect' => '/purchase'
         )));
    }
    }  

  function validationAction(){
    $connected = is_object($this->get('security.context')->getToken()->getUser());
    if($connected){
      return $this->purchaseAction($validation = 1);
    } else return ($this->render('LPCISIIPHP2012Bundle:Default:index.html.twig'));
  }
  
  function deleteAction($idCo){
    $user = $this->get('security.context')->getToken()->getUser();
    $connected = is_object($user);
    $em = $this->getDoctrine()->getEntityManager();
    
    if($connected && $idCo){
      $commande = $em->getRepository('LPCISIIPHP2012Bundle:Commandes')
	->getCommandeById($idCo, $user->getIdU());
      $v = $this->container->get('doctrine')->getEntityManager();
      $v->remove($commande[0]);
      $v->flush();

      return $this->purchaseAction(2);
    }
  }

}